<?php

declare (strict_types = 1);

namespace app\admin\controller\auth;


use think\response\Json;
use app\admin\controller\Controller;

class Admin extends Controller
{
    protected $service = null;

    public function initialize()
    { 
        parent::initialize();
        $this->service = new \app\admin\service\auth\AdminService();
    }

    /**
     * 列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        event('UserLogin', ['id'=>1]);
        return success($this->service->list());
    }

    /**
     * 详情
     * @param int $id
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Dengje\Pxy\exception\BaseException
     */
    public function detail(int $id): Json
    {
        return success($this->service->detail($id));
    }

    /**
     * 添加
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Dengje\Pxy\exception\BaseException
     */
    public function create(): Json
    {
        return success('创建成功',$this->service->create(request()->param()));
    }

    /**
     * 编辑
     * @param int $id
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Dengje\Pxy\exception\BaseException
     */
    public function update(): Json
    {
        $id = request()->get('id');
        return success('更新成功',$this->service->update($id,request()->param()));
    }

    public function delete(): Json
    {
        $id = request()->get('id');
        return success('删除成功',$this->service->delete($id));
    }

    public function updatePassword()
    {
        $data = request()->param();
        $adminId = request()->admin_id;
        $this->service->updatePassword($adminId,$data);
        return success('更新成功');
    }
}